<?php
/**
 * Created by PhpStorm.
 * User: zjw
 * Date: 2018/5/23
 * Time: 上午9:52
 */

namespace common\jobs;

use common\models\Coupon;
use common\service\HttpRequestService;
use yii\base\BaseObject;
use yii\queue\Queue;
use yii\queue\RetryableJobInterface;

/**
 *  同步远程数据
 * Class LoadCouponDataJob
 * @package common\jobs
 */
class LoadCouponDataJob extends BaseObject implements RetryableJobInterface
{

    public $couponCategoryMarkId;

    /**
     * @param Queue $queue which pushed and is handling the job
     */
    public function execute($queue)
    {
        $baseUrl = getenv("ISSURE_COUPON_API");
        $url = $baseUrl . "/api/coupon/" . $this->couponCategoryMarkId;
        try {
            $res = HttpRequestService::sendGetRequest($url);
            if ($res->code == 1 && !empty($res->data)) {
                $dataList = $res->data;
                if (count($dataList) > 0) {
                    foreach ($dataList as $key => $data) {
                        $coupon = Coupon::find()->where(['origin_id' => $data->id])->one();
                        if ($coupon == null) {
                            $coupon = new Coupon();
                            $coupon->status = Coupon::STATUS_VALID;
                            $coupon->use_num = $data->use_num;
                        }
                        $coupon->code = $data->code;
                        $coupon->origin_id = $data->id;
                        $coupon->origin_num = $data->origin_num;
                        $coupon->catalog = $data->catalog;
                        $coupon->catalog_mark_id = $this->couponCategoryMarkId;
                        $coupon->created_at = $data->created_at;
                        $coupon->updated_at = $data->updated_at;
                        $coupon->expire_at = $data->expire_at;
                        $coupon->name = $data->name;
                        $coupon->consumption_amount = $data->consumption_amount;
                        $coupon->discount_amount = $data->discount_amount;
                        $coupon->password = $data->password;
                        $coupon->desc = $this->getdesc();

                        $coupon->save();
                        echo $coupon->id;
                    }
                }
                return true;
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }
    private function getdesc(){
        $baseUrl = getenv("ISSURE_COUPON_API");
        $url = $baseUrl . "/api/coupon_category?mark_id=" . $this->couponCategoryMarkId;
        try {
            $res = HttpRequestService::sendGetRequest($url);
            if ($res->code == 1 && !empty($res->data)) {
                $dataList = $res->data;
                if (count($dataList) > 0) {
                    foreach ($dataList as $key => $data) {
                        return json_encode($data->description);
                    }
                }
                return false;
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }
    /**
     * @return int time to reserve in seconds
     */
    public function getTtr()
    {
        return 0;
    }

    /**
     * @param int $attempt number
     * @param \Exception $error from last execute of the job
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        return $attempt < 5;
    }
}
